import Player from "../model/player/player.js";
import DateUtil from "../util/date.js";
import REDIS from "../util/redis.js";
import Fight from "./fight.js";



/**
 * 打劫事件
 */
export default class Rob {
	robber: Player;
	target: Player;

	cd: number = 120;
	hurtCd = 240;

	/**
	 * 
	 * @param {Player} robber 打劫者
	 * @param {Player} target 目标
	 */
	constructor(robber: Player, target: Player) {
		this.robber = robber;
		this.target = target;
	}

	async do() {
		//如果正在冷却
		if (await this.isCooling()) return {
			msg: `打劫太频繁了！${Math.round(await this.remainingCdTime())}秒后才能再次打劫！`,
			success: false,
			records: [],
			winner: null
		};
		//如果对方被打劫正在冷却
		if (await this.isHurtCooling()) return {
			msg: `【${this.target.nick}】因为不久前被打劫过一次提高警惕躲了起来！你未能得逞`,
			success: false,
			records: [],
			winner: null
		};
		//如果有人已死
		if (this.robber.currentHp <= 0) return {
			msg: `你都死了就不能安分一点吗`,
			success: false,
			records: [],
			winner: null
		};
		if (this.target.currentHp <= 0) return {
			msg: `他都死了你就放过他吧`,
			success: false,
			records: [],
			winner: null
		};
		//如果打劫自己
		if (this.robber == this.target) return {
			msg: `打劫自己是吧`,
			success: false,
			records: [],
			winner: null
		};


		let now = new DateUtil().secondStamp;
		await REDIS.set(`LNE:records:rob:${this.robber.qqNum}:rob_time`, now);

		let fight = new Fight(this.target, this.robber);

		let s = fight.fight(false);
		if (s.status.dodgeSuccess) return {
			msg: `你偷偷溜过去，结果却被对方发现了，你白忙一趟`,
			success: false,
			records: [],
			winner: null
		};

		await REDIS.set(`LNE:records:rob:${this.target.qqNum}:hurt_time`, now);

		let records = [s];

		let result: { records: any; winner: any; msg?: string; success?: boolean; };
		try {
			if (!fight.haveDeath()) result = fight.duel();
		} catch (err) { }
		result.records.forEach(a => records.push(a));



		let r = {
			success: true,
			msg: "",
			records: records,
			winner: result.winner
		};
		let c = this.balance();
		if (result.winner == this.robber) r.msg = "成功打劫了对方，得到了" + c + "个硬币！";
		else r.msg = "偷鸡不成蚀把米，反而被对方按在地上摩擦，失去了" + c + "个硬币！";

		return r;
	}

	balance() {
		if (this.target.currentHp <= 0) return trunCoin(this.target, this.robber);
		return trunCoin(this.robber, this.target);
	}


	async isHurtCooling() {
		return (await this.remainingHurtCdTime()) > 0;
	}

	async isCooling() {
		return (await this.remainingCdTime()) > 0;
	}

	async remainingHurtCdTime() {
		let lastTime = await REDIS.get(`LNE:records:rob:${this.target.qqNum}:hurt_time`);
		lastTime = Number.parseInt(lastTime);
		return lastTime + this.hurtCd - new DateUtil().secondStamp;
	}

	async remainingCdTime() {
		let lastTime = await REDIS.get(`LNE:records:rob:${this.robber.qqNum}:rob_time`);
		lastTime = Number.parseInt(lastTime);
		return lastTime + this.cd - new DateUtil().secondStamp;
	}


}

/**
 * p1的钱转移到p2
 * @param {Player} p1 
 * @param {Player} p2 
 */
function trunCoin(p1: Player, p2: Player) {
	let avg = (p1.coin + p2.coin) / 2;
	let c = Math.floor(Math.min(p1.coin, avg) / (10 + Math.random() * 10));
	p1.coin -= c;
	p2.coin += c;
	return c;
}

